define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/client/toPay.html",
    "wTools",
    "area",
    "validate"

], function ($, _, Backbone, template) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        events: {
        },
        initialize: function () {
            $("#head_back").hide();
            if(wData.urlData.wxImgUrl!=undefined&&wData.urlData.wxImgUrl!=null&&wData.urlData.wxImgUrl!=""){
                wData.wxImgUrl= eval("(" + unescape(wData.urlData.wxImgUrl) + ")");
                this.render();
                var that=this;
                setTimeout(function(){
                    that.pay();
                }, 1000);
            }
            else{
                alert("支付异常,请重试");
            }
        },
        back: function () {
            history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        onBridgeReady: function () {
            try {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest', {
                        "appId": wData.wxImgUrl.jsapi_appid,     //公众号名称，由商户传入
                        "timeStamp": wData.wxImgUrl.jsapi_timestamp,         //时间戳，自1970年以来的秒数
                        "nonceStr": wData.wxImgUrl.jsapi_noncestr, //随机串
                        "package": wData.wxImgUrl.jsapi_package,
                        "signType": wData.wxImgUrl.jsapi_signtype,         //微信签名方式:
                        "paySign": wData.wxImgUrl.jsapi_paysign //微信签名
                    },
                    function (res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            location.hash="paySucc";
                            //调用 接口 更改 订单 和 通知单状态;
                        } else if (res.err_msg == "get_brand_wcpay_request:cancel") {

                            wTips("支付取消");
                            location.hash="pay";
                        } else {
                            //支付失败页面条状
                            wTips("支付失败");
                            location.hash="pay";
                        }
                    }
                );
            }
            catch (e) {
                alert("errorMsg:"+e.message+",desc:"+e.description);
            }
        },
        pay: function () {
            try {
                if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                        document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(), false);
                    } else if (document.attachEvent) {
                        document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady());
                        document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady());
                    }
                } else {
                    this.onBridgeReady();
                }
            }
            catch (e) {
                alert("errorMsg:"+e.message+",desc:"+e.description);
            }
        },

        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            delete this;
        }
    });
});